package com.my.vote.pst.info;

import android.text.TextUtils;

import com.my.vote.VoteApplication;
import com.my.vote.entity.EUser;
import com.my.vote.entity.EVoteInfo;
import com.my.vote.entity.EVoteItem;
import com.my.vote.entity.req.ReqAddVote;
import com.sfh.lib.AppCacheManager;
import com.sfh.lib.mvp.service.AbstractPresenter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * 功能描述:
 *
 * @author
 * @date 2018/4/12
 */
public class PstAddVote extends AbstractPresenter<IPstAddVote.IVVote> implements IPstAddVote {


    @Override
    public void addVote(final ReqAddVote info) {

        final EVoteInfo voteInfo = new EVoteInfo();
        final EUser user = AppCacheManager.newInstance().getCache(VoteApplication.USER, new EUser());
        voteInfo.setUserId(user.getObjectId());
        voteInfo.setType(info.getType());
        voteInfo.setVoteUrl(info.getVoteUrl());
        voteInfo.setTitle(info.getTitle());
        voteInfo.setIntroduce(info.getIntroduce());
        voteInfo.setVoteType(info.getVoteType());

        voteInfo.setEnrollEndTime(info.getEnrollEndTime() != null ? info.getEnrollEndTime() : 0);
        voteInfo.setVoteEndTime(info.getVoteEndTime() != null ? info.getVoteEndTime() : 0);

        voteInfo.setVotingPersonSize(0);
        voteInfo.setVisitSize(0);
        voteInfo.setCandidateSize(info.getItem().size());

        getView().showLoading(false);
        voteInfo.save(AppCacheManager.getApplication(), new SaveListener() {
            @Override
            public void onSuccess() {
                //1 可以参加报名模式， 2 只能投票模式
                if (TextUtils.equals("2", info.getType())) {
                    List<BmobObject> objects = new ArrayList<>(info.getItem().size());
                    for (EVoteItem item : info.getItem()) {
                        item.setCreateUserId(user.getObjectId());
                        item.setVoteId(voteInfo.getObjectId());
                        objects.add(item);
                    }
                    new BmobObject().insertBatch(AppCacheManager.getApplication(), objects, new SaveListener() {
                        @Override
                        public void onSuccess() {
                            getView().hideLoading();
                            getView().onSuccess();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            voteInfo.delete(AppCacheManager.getApplication());
                            getView().onFial("add  fail!");
                        }
                    });
                } else {
                    getView().hideLoading();
                    getView().onSuccess();
                }
            }

            @Override
            public void onFailure(int i, String s) {
                getView().hideLoading();
                getView().onFial("add  fail!");
            }
        });
    }

    @Override
    public void updateFile(String file) {

        getView().showLoading(true);
        final BmobFile bmobFile = new BmobFile(new File(file));
        bmobFile.upload(AppCacheManager.getApplication(), new UploadFileListener() {


            @Override
            public void onSuccess() {
                getView().hideLoading();
                //bmobFile.getFileUrl()--返回的上传文件的完整地址
                getView().onFileSuccess(bmobFile.getFileUrl(AppCacheManager.getApplication()));

            }

            @Override
            public void onFailure(int i, String s) {
                getView().hideLoading();
                getView().onFileFial("file update fail!");
            }
        });
    }
}
